package com.zxlfysj.linkedlist;

/**
 * 双向链表
 * @author yangshujing
 * @create 2020-08-15 12:21
 */
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        System.out.println("双向链表的测试");
        HeroNode2 hero1 = new HeroNode2(1, "潘森", "战争之王");
        HeroNode2 hero2 = new HeroNode2(2, "贾克斯", "武器大师");
        HeroNode2 hero3 = new HeroNode2(3, "拉克丝", "光辉女郎");
        HeroNode2 hero4 = new HeroNode2(4, "安妮", "黑暗之女");
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero4);
        doubleLinkedList.list();
        doubleLinkedList.del(4);
        doubleLinkedList.list();
    }

}

class DoubleLinkedList {
    //初始化一个头节点，头节点不要动，不存放具体的数据
    private HeroNode2 head = new HeroNode2(0, "", "");

    //返回头节点
    public HeroNode2 getHead() {
        return head;
    }

    //遍历双向链表，跟单链表遍历一样
    public void list() {
        if(head.next == null) {
            System.out.println("链表空");
        }
        HeroNode2 temp = head.next;
        while (true) {
            if(temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    //添加节点
    public void add(HeroNode2 heroNode) {
        //head节点不能动，使用辅助指针temp
        HeroNode2 temp = head;
        //遍历找到当前链表的最后节点
        while (true) {
            if(temp.next == null) {
                break;
            }
            //辅助指针后移
            temp = temp.next;
        }
        //将最后节点的next指向新的节点
        temp.next = heroNode;
        //将新添加的节点的pre指向最后节点
        heroNode.pre = temp;
    }

    ///修改节点信息，跟单链表的方法一样
    public void update(HeroNode2 newHero) {
        if(head.next == null) {
            System.out.println("链表空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if(temp == null) {
                break;
            }
            if(temp.no == newHero.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {
            temp.name = newHero.name;
            temp.nickname = newHero.nickname;
        } else {
            System.out.println("没有找到");
        }

    }
    //删除节点，双向链表可以自我删除，直接找到要删除的节点
    public void del(int no) {
        if(head.next == null) {
            System.out.println("链表空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if(temp == null) {
                break;
            }
            if(temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag) {

            temp.pre.next = temp.next;
            if(temp.next != null) { //如果要删除的是最后一个节点，要特殊处理
                temp.next.pre = temp.pre;
            }

        } else{
            System.out.println("没有找到");
        }

    }

}



//定义HeroNode2，每个HeroNode对象就是一个节点
class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;//指向下一个节点
    public HeroNode2 pre; //指向前一个节点

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
